using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Pyramid
{
    class CatmullRom: Multiresolution
    {
        public override ImageWithDetail DownSample(ImageWithDetail dimage)
        {
            int i, j;
            double[] r;
            GImage image = dimage.TheImage;
            GImage FinalImage = new GImage(image.Width / 2, image.Height / 2);
            ImageWithDetail rimage = new ImageWithDetail(dimage);
         

            //decrease the resolution in horizontal direction
            GImage RowImage = new GImage(image.Width / 2, image.Height);
            for (j = 0; j < image.Height; j++) 
                for (i = 0; i < image.Width; i += 2)                
                {

                    r = Find_NewValue(image, new double[8] { -3, -9, 29, 111, 111, 29, -9, -3 }, new int[2] { i, j }, 0);
                    RowImage.SetPixel(i/2, j,r);
                }

            //decrease the resolution in vertical direction
            for (i = 0; i < RowImage.Width; i++)
                for (j = 0; j < RowImage.Height; j+=2)
                {
                    r = Find_NewValue(RowImage, new double[8] { -3, -9, 29, 111, 111, 29, -9, -3 }, new int[2] { i, j }, 1);
                    FinalImage.SetPixel(i, j/2, r);

                  
                }
            rimage.TheImage = FinalImage;
            return rimage;
        }

        public override ImageWithDetail UpSample(ImageWithDetail dimage)
        {
            int i, j;          
            double[] r;
            GImage image = dimage.TheImage;
            GImage FinalImage = new GImage(image.Width * 2, image.Height * 2);

            //increase the resolution in horizontal direction
            GImage RowImage = new GImage(image.Width * 2, image.Height);
            for (j = 0; j < image.Height; j++) 
                for (i = 0; i < image.Width; i++)
                
                {

                    r = Find_NewValue(image, new double[4] { -9, 111, 29, -3 }, new int[2] { i, j }, 0);
                    RowImage.SetPixel(2*i,j,r);

                    r = Find_NewValue(image, new double[4] { -3, 29, 111, -9 }, new int[2] { i, j }, 0);
                    RowImage.SetPixel(2 * i+1, j, r);
                }

            //increase the resolution in vertical direction
            for (i = 0; i < RowImage.Width; i++)
                for (j = 0; j < RowImage.Height; j++)
                {
                    r = Find_NewValue(RowImage, new double[4] { -9, 111, 29, -3 }, new int[2] { i, j }, 1);
                    FinalImage.SetPixel(i, 2 * j, r);

                    r = Find_NewValue(RowImage, new double[4] { -3, 29, 111, -9 }, new int[2] { i, j }, 1);                        
                    FinalImage.SetPixel(i, 2*j+1,r);
                }
            dimage.TheImage = FinalImage;
            return dimage;
        }
       

    }
}
